package com.ejuornal.timer;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Ads;
import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Category;
import com.ejuornal.bean.PerPeriodical;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.SystemInfo;
import com.ejuornal.service.AdsService;
import com.ejuornal.service.ArticalService;
import com.ejuornal.service.CategoryService;
import com.ejuornal.service.PerPeriodicalService;
import com.ejuornal.service.SystemInfoService;
import com.ejuornal.springutil.SpringUtil;
import com.ejuornal.util.PropertyMgr;
import com.ejuornal.util.SubStringHTML;
import com.ejuornal.util.VelocityUtil;
@Component("indexTimer")
public class IndexTimer {
    private int articaCount = 20;
    private int periodicalCount = 5;
    private int perPeriodicalCount = 10;
//     @Scheduled(cron="30 * * * * ?")	 //更新时间
  //   @Scheduled(cron="0 0 1 * * ?")	//每天一点执行该方法
    public void index() throws IOException {
        ArticalService as = (ArticalService) SpringUtil.getBean("articalService");
        PerPeriodicalService pps = (PerPeriodicalService) SpringUtil.getBean("perPeriodicalService");
        AdsService adsService = (AdsService) SpringUtil.getBean("adsService");
        CategoryService cs = (CategoryService)SpringUtil.getBean("categoryService");
        SystemInfoService sysInfoService = (SystemInfoService)SpringUtil.getBean("systemInfoService");
        
        //网站关键字
        SystemInfo keywords = sysInfoService.findById(1);
        //网站简介
        SystemInfo introduceInfo = sysInfoService.findById(2);
        
        List<Category> categorys = cs.findAll();
        List<PerPeriodical> perPeriodicals_0 = pps.findNew(perPeriodicalCount);
        for (PerPeriodical perPeriodical : perPeriodicals_0) {
			String periodicalStr = perPeriodical.getPeriodical().getName();
			String periodicalStrOver = SubStringHTML.subStringHTML(periodicalStr, 12, "..");
			Periodical periodical = new Periodical();
			periodical.setName(periodicalStrOver);
			periodical.setCover(perPeriodical.getPeriodical().getCover());
			perPeriodical.setPeriodical(periodical);
		}
        
        List<PerPeriodical> perPeriodicals_1 = pps.findTop(periodicalCount);
        for (PerPeriodical perPeriodical : perPeriodicals_1) {
			String str = perPeriodical.getIntroduce();
			String strOver = SubStringHTML.subStringHTML(str, 60, "...");
			perPeriodical.setIntroduce(strOver);
		}
        
        List<Artical> articals = as.findTop(articaCount);
        Ads ads_center = adsService.findAdsByPosIdAndTime(1);
		if(ads_center == null){
			ads_center = new Ads();
			ads_center.setPicSrc("attached/images/adsPic.jpg");
			ads_center.setUrl("javascript:void(0)");
		}
        Ads ads_bottom = adsService.findAdsByPosIdAndTime(2);
        if(ads_bottom==null){
        	ads_bottom = new Ads();
        	ads_bottom.setPicSrc("attached/images/adsPic.jpg");
        	ads_bottom.setUrl("javascript:void(0)");
		}
        
        String vm = "vm/index.vm";
        VelocityContext context = new VelocityContext();
        context.put("keywords", keywords);
        context.put("introduceInfo", introduceInfo);
        context.put("categorys", categorys);
        context.put("perPeriodicals_0", perPeriodicals_0);
        context.put("perPeriodicals_1", perPeriodicals_1);
        context.put("articals", articals);
        context.put("ads_center", ads_center);
        context.put("ads_bottom", ads_bottom);
        Properties p = new Properties();
        PropertyMgr.getInstance("/vm/vm.properties").get("index_path");
        String filePath = "d:/index2.jsp";
        VelocityUtil.init_1(context, vm,  (String)PropertyMgr.getInstance("/vm/vm.properties").get("index_path"));
    }
    
    public static void main(String[] args) throws IOException {
        IndexTimer it = new IndexTimer();
        it.index();
    }
}
